package controllers

import (
	"encoding/json"
	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/services"
)

type CuringController struct {
	BaseController
}

func (c *CuringController) Prepare() {
	c.BaseController.Prepare()

	c.Data["activeSidebarUrl"] = "/curing/plan_list"
}

//养护计划
func (c *CuringController) PlanList() {
	c.checkAuthor("curing/plan_list")
	if c.Ctx.Input.IsPost() {
		//获取数据列表和总数
		var params models.ErpCuringQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Depid = c.curUser.DepartmentId
		data, total := models.ErpCuringPageList(&params)

		c.jsonDataTable(total, data)
	}
	c.display("", "curing/planlist_footerjs.html")
}

//新增养护计划
func (c *CuringController) PlanCreate() {
	c.checkAuthor("curing/plan_list")
	if c.Ctx.Input.IsPost() {
		curing := new(models.ErpCuring)
		if err := c.ParseForm(curing); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		curing.DepartmentId = c.curUser.DepartmentId
		ser := services.CuringService{}
		if id, err := ser.CuringCreate(curing); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		} else {
			c.jsonResult(enums.JRCodeSucc, "ok", id)
		}
	}
	//获取品牌
	brand := models.ErpBrandList(&models.ErpBrandQueryParam{DepartmentId: c.curUser.DepartmentId})
	c.Data["brand"] = brand

	c.display("", "curing/plancreate_footerjs.html", "shared/layout_dialog.html")
}

//养护产品列表
func (c *CuringController) CuringProductList() {
	c.checkAuthor("curing/plan_list")
	curing_id, _ := c.GetInt64(":id")
	if curing_id == 0 {
		c.pageError("参数错误")
	}
	if c.Ctx.Input.IsPost() {
		//获取需要养护的数据
		var params models.ErpCuringDataQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Depid = c.curUser.DepartmentId
		params.Status = []int{0, 1}
		data, total := models.ErpCuringDataPageList(&params)

		gather_data := make(map[string]interface{})
		gather_data["curing_data"] = data
		if params.Offset == 0 { //获取计划信息
			curing, _ := models.GetErpCuringById(curing_id)
			gather_data["curing"] = curing
		}
		c.jsonDataTable(total, gather_data)
	}

	c.display("", "curing/curingproductlist_footerjs.html")
}

//设置产品养护状态
func (c *CuringController) SetProductCuringDataStatus() {
	curing_id, _ := c.GetInt64("curing_id")
	curing_data_id := c.GetString("curing_data_id")
	if curing_id == 0 {
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
	}
	if len(curing_data_id) <= 0 {
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
	}
	var data_id []int64
	if err := json.Unmarshal([]byte(curing_data_id), &data_id); err != nil {
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
	}
	ser := services.CuringService{}
	if err := ser.SetProductCuringDataStatus(curing_id, data_id); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", curing_id)
}

//养护商品检查
func (c *CuringController) CuringProductCheck() {
	c.checkAuthor("curing/plan_list")
	curing_id, _ := c.GetInt64(":id")
	if curing_id == 0 {
		c.pageError("参数错误")
	}
	if c.Ctx.Input.IsPost() {
		//获取需要养护的数据
		var params models.ErpCuringDataQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Depid = c.curUser.DepartmentId
		params.Status = []int{1}
		data, total := models.ErpCuringDataPageList(&params)

		gather_data := make(map[string]interface{})
		gather_data["curing_data"] = data
		if params.Offset == 0 { //获取计划信息
			curing, _ := models.GetErpCuringById(curing_id)
			gather_data["curing"] = curing
		}
		c.jsonDataTable(total, gather_data)
	}
	c.display("", "curing/curingproductcheck_footerjs.html")
}

//保存养护商品检查
func (c *CuringController) CuringProductCheckSave() {
	c.checkAuthor("curing/plan_list")
	curing_id, _ := c.GetInt64("curing_id")
	exterior, _ := c.GetInt("exterior")
	packing, _ := c.GetInt("packing")
	opinion, _ := c.GetInt("opinion")
	var curing_data_id []int64
	c.Ctx.Input.Bind(&curing_data_id, "curing_data_id")

	ser := services.CuringService{}
	if err := ser.CuringProductCheckSave(curing_id, exterior, packing, opinion, curing_data_id); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", "")
}

//查看养护记录
func(c *CuringController) CuringProductCheckDetail() {
	c.checkAuthor("curing/plan_list")
	curing_id, _ := c.GetInt64(":id")
	if curing_id == 0 {
		c.pageError("参数错误")
	}
	if c.Ctx.Input.IsPost() {
		//获取需要养护的数据
		var params models.ErpCuringDataQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Depid = c.curUser.DepartmentId
		params.Status = []int{2}
		data, total := models.ErpCuringDataPageList(&params)

		gather_data := make(map[string]interface{})
		gather_data["curing_data"] = data
		if params.Offset == 0 { //获取计划信息
			curing, _ := models.GetErpCuringById(curing_id)
			gather_data["curing"] = curing
		}
		c.jsonDataTable(total, gather_data)
	}
	c.display("", "curing/curingproductcheckdetail_footerjs.html")
}
